const state = {
  typePage: {}, //题目类型
  typeList: [],
  questionnaireList: [], // 问卷列表
  questionnairePage: {},
  subjectList: [], //题目列表
}

const actions = {
  saveTypes: ({ commit }, payload) => {
    commit('SAVETYPES', payload)
  },
  saveList: ({ commit }, payload) => {
    commit('SAVELIST', payload)
  },
  saveSubjectList: ({ commit }, payload) => {
    commit('SAVESUBJECTLIST', payload)
  }
}

const mutations = {
  SAVETYPES: (state, payload) => {
    state.typeList = payload.result
    state.typePage = {
      number: payload.number,
      total: payload.resultNum,
      size: 10
    }
  },
  SAVELIST: (state, payload) => {
    if (payload.result.length === 0) {
      state.questionnaireList = []
    } else {
      state.questionnaireList = payload.result.map(item => {
        return {
          id: item.id,
          name: item.name,
          description: item.description || '',
          state: item.state,
          options: []
        }
      })
    }
    state.questionnairePage = {
      number: payload.number,
      total: payload.resultNum,
      size: 10
    }
  },
  SAVESUBJECTLIST: (state, payload) => {
    if (payload.result.length === 0) {
      state.subjectList = []  
    } else {
      state.subjectList = payload.result.map(item => {
        return {
          id: item.id,
          name: item.name,
          qTypeId: item.q_type_id,
          isAnswerRequired: Number(item.is_answer_required),
          orderNumber: item.order_number,
          optionList: item.optionList,
          state: item.state
        }
      })
    }
  }
}

export default {
  state,
  actions,
  mutations
}